<?php
namespace Home\Controller;
class ClasssettingController extends BackController {
	public function holidaySet(){
		$holiday = M('holiday');
		if(IS_POST){
			$_POST['xid'] = session('qe_user')['xid'];
			if($holiday->create()) {
					if($holiday->add($_POST)) {
							echo 1;exit;
					}else {
							echo 0;exit;
					}
			}else {
					echo 0;exit;
			}
		}else{
			$holiday_data = $holiday->order('id desc')->select();
			$this->assign('holiday_data',$holiday_data);
			//var_dump($holiday_data);
			$this->display();
		}
	}
	public function search(){
		if(IS_POST){
			$id = I('post.id');
			$holiday = M('holiday')->where('id='.$id)->find();
			$this->ajaxReturn($holiday);
		}
	}
	public function edit(){
		if(IS_POST){
			$holiday = M('holiday');
			$id = I('post.id');
			$data['holiday_name'] = I('post.holiday_name','','addslashes');
			$data['start_time'] = I('post.start_time','','addslashes');
			$data['next_time'] = I('post.next_time','','addslashes');
			if($holiday->create()) {
					if($holiday->where('id='.$id)->save($data) !== false) {
							$this->success('编辑成功',U('Classsetting/holidaySet'));
					}else {
							$this->error('编辑失败',U('Classsetting/holidaySet'));
					}
			}
		}
	}
	public function classroomSet(){
		$room = D('Room');
		if(IS_POST){
			$_POST['xid'] = session('qe_user')['xid'];
			$_POST['state'] = 0;
			if($room->create()) {
					if($room->add($_POST)) {
							$this->success('新增成功',U('Classsetting/classroomSet'));
					}else {
							$this->error('新增失败',U('Classsetting/classroomSet'));
					}
			}
		}else{
			$roomname = I('get.room_name_search','','addslashes');
			$where['roomname'] = array('like',"%$roomname%");
			if(session('qe_user')['xid']!=0){
				$where['xid'] = array('eq',session('qe_user')['xid'],'and');
			}
			$room_data = $room->where($where)->order('id desc')->select();
			foreach($room_data as $k=>$v){
				$room_data[$k]['x_name'] = $this->basname($v['xid']);
			}
			$this->assign('room_data',$room_data);
			$this->display();
		}
	}
	public function roomsearch(){
		if(IS_POST){
			$id = I('post.id');
			$room = M('room')->where('id='.$id)->find();
			$room['xname'] = $this->basname($room['xid']);
			$this->ajaxReturn($room);
		}
	}
	public function roomedit(){
		if(IS_POST){
			$room = M('room');
			$id = I('post.id');
			$data['roomnumber'] = I('post.roomnumber','','addslashes');
			$data['roomname'] = I('post.roomname','','addslashes');
			$data['capacity'] = I('post.capacity','','addslashes');
			$data['state'] = I('post.state','','addslashes');
			if($room->create()) {
					if($room->where('id='.$id)->save($data) !== false) {
							$this->success('编辑成功',U('Classsetting/classroomSet'));
					}else {
							$this->error('编辑失败',U('Classsetting/classroomSet'));
					}
			}
		}
	}
	public function schedule_class(){
		$id = I('get.id','','int');
		$class = M('gradesclass')->where('id='.$id)->find();
		//开班星期
		$week_id = json_decode($class['week_id'],true);
		foreach($week_id as $v){
			$ss[] = $this->getAllMondayOf($class['start_time'],$class['end_time'],$v);
		}
		$schedule = M('schedule');
		$all_schedule = $schedule->where('class_id='.$class['id'])->select();
		if(!empty($all_schedule)){
			foreach($all_schedule as $vv){
				if($schedule->where('id='.$vv['id'])->delete()) {
						$state = true;
				}else {
						$state = false;
				}
			}
		}else{
			$state = true;
		}
		if($state == true) {
			foreach($ss as $v){
				foreach($v as $val){
					if($val>=$class['start_time'] and $val<=$class['end_time']){
						$state = $this->class_schedule($val,$class['id'],$class['classname']);
					}
				}
			}
		}
		if($state == true) {
				$this->success('自动排班完成',U('Classsetting/classList'));
		}else{
				$this->error('自动排班失败',U('Classsetting/classList'));
		}
	}
	public function setupclass(){
		$class = M('gradesclass');
		if(IS_POST){
			$id = I('post.id','','int');
			$where['classname'] = array('eq',I('post.classname'),'and');
			$where['xid'] = array('eq',session('qe_user')['xid'],'and');
			/*$where['start_time'] = array('eq',I('post.classname'),'and');
			$where['end_time'] = array('eq',I('post.classname'),'and');*/
			$_POST['week_id'] = json_encode(I('post.week_id','','addslashes'));
			$count = $class->where($where)->count();
			if($count>=2){
				$this->error('该班级名称与开课时间已经存在2条记录，不可重复添加!',U('Classsetting/classList'));
			}else{
				if(empty($id)){
					if($class->create()) {
							if($class->add($_POST)) {
									$this->success('新增成功',U('Classsetting/classList'));
							}else {
									$this->error('新增失败',U('Classsetting/classList'));
							}
					}else{
						$this->error($class->getError());
					}
				}else{
					if($class->create()) {
							if($class->where('id='.$id)->save($_POST) !== false) {
									$this->success('编辑成功',U('Classsetting/classList'));
							}else {
									$this->error('编辑失败',U('Classsetting/classList'));
							}
					}else {
							$this->error($class->getError());
					}
				}
			}
		}else{
			$id = I('get.id','','int');
			if($id != ''){
				$class_data = $class->where('id='.$id)->find();
				$class_data['week_id'] = json_decode($class_data['week_id'],true);
				$this->assign('class_data',$class_data);
			}
			
			$other = $this->all_other();
			$this->assign('other',$other);
			$curriculum = M('curriculum')->select();
			$this->assign('curriculum',$curriculum);
			$staff = $this->all_staff(session('qe_user')['xid']);
			$this->assign('staff',$staff);
			
			$room = $this->all_room(session('qe_user')['xid']);
			$this->assign('room',$room);
			//var_dump($class_data['week_id']);
			$this->display();
		}
	}
	public function classList(){
		$class = M('gradesclass');
		$where="";
		if(session('qe_user')['xid']!=0){
			$where['xid'] = array('eq',session('qe_user')['xid'],'and');
		}
		$count      = $class->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$this->all_count(1));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$class_list = $class->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($class_list as $k=>$v){
			$number = 0;
			//课程分类
			$class_list[$k]['lecturename'] = $this->curname($v['lecture']);
			//主讲老师
			$class_list[$k]['teachername'] = $this->teachername($v['teacher_id']);
			//助教
			$class_list[$k]['headmastername'] = $this->teachername($v['headmaster_id']);
			//校区
			$class_list[$k]['xname'] = $this->basname($v['xid']);
			//班级已排人数
			$student = M('sc_student')->where('up=1 and curriculum_id='.$v['lecture'].' and class_id='.$v['id'])->select();
			foreach($student as $val){
				$number = $number+1;
			}
			$class_list[$k]['number'] = $number;
			$class_list[$k]['percentage'] = ceil($class_list[$k]['number']/$class_list[$k]['enrollment_number']*100);
		}
		//var_dump($where);
		$this->assign('class_list',$class_list);
		$this->display();
	}
	public function schedule(){
		$other = M('other');
		if(session('qe_user')['xid']==0){
			$where['pid'] = array('eq',1,'and');
		}else{
			$where['id'] = array('eq',session('qe_user')['xid'],'and');
		}
		$campus = $other->where($where)->select();
		$this->assign('campus',$campus);
		//var_dump(date("w",strtotime("2018-08-26")));
		$this->display();
	}
	public function campus(){
		$class = M('gradesclass');
		if(IS_POST){
			$campus_id = I('post.campus_id','','int');
			$classroom = $class->where('xid='.$campus_id)->select();
			$this->ajaxReturn($classroom);
		}
	}
	public function schedule_data(){
		$schedule = D('ScheduleView');
		if(IS_POST){
			$class_id = I('post.id','','int');
			$data = $schedule->where('class_id='.$class_id)->select();
			foreach($data as $k=>$v){
				$data[$k]['headmaster_name'] = $this->teachername($v['headmaster_id']);
			}
			$this->ajaxReturn($data);
		}
	}
	public function schedule_show(){
		if(IS_POST){
			$attendance = M('attendance');
			$all = I('post.all','','addslashes');
			$data['class_id'] = I('post.class_id','','int');
			$data['date_id'] = I('post.date_id','','int');
			$data['state_id'] = I('post.state_id','','int');
			$data['content'] = I('post.content','','addslashes');
			$data2['state_id'] = I('post.state_id','','int');
			$data2['content'] = I('post.content','','addslashes');
			$where['class_id'] = array('eq',$data['class_id'],'and');
			$where['date_id'] = array('eq',$data['date_id'],'and');
			$time = I('post.time','','addslashes');
			$news_time = date('Y-m-d');
			if($time>$news_time){
				$this->error('今天不是上课时间，不能执行考勤!',U('Classsetting/schedule_show?id='.$data['date_id']));
			}else if($time<$news_time){
				$this->error('上课时间已经截止，不能执行考勤!',U('Classsetting/schedule_show?id='.$data['date_id']));
			}else if($time==$news_time){
				foreach($all as $v){
					$data['student_id'] = $v;
					$where['student_id'] = array('eq',$data['student_id'],'and');
					if($attendance->create()) {
						if(!$attendance->where($where)->find()){
							if($attendance->add($data)) {
									$ss = true;
							}else {
									$ss= false;break;
							}
						}else{
							if($attendance->where($where)->save($data2) !== false) {
									$ss = true;
							}else{
									$ss= false;break;
							}
						}
					}else{
						$this->error($attendance->getError());
					}
				}
				if($ss == true) {
						$this->success('执行考勤成功',U('Classsetting/schedule_show?id='.$data['date_id']));
				}else{
						$this->error('执行考勤失败',U('Classsetting/schedule_show?id='.$data['date_id']));
				}
			}
		}else{
			//上课时间
			$id = I('get.id','','int');
			$data = M('gradesclass as g')->join('schedule as s on s.class_id = g.id')->where('s.id='.$id)->find();
			$data['student'] = M('sc_student')->where('class_id='.$data['class_id'].' and curriculum_id='.$data['lecture'])->select();
			$state = M('other')->where('pid=28')->select();
			$where['class_id'] = array('eq',$data['class_id'],'and');
			$where['date_id'] = array('eq',$data['id'],'and');
			foreach($data['student'] as $k=>$v){
				$where['student_id'] = array('eq',$v['id'],'and');
				$student_show = M('attendance')->where($where)->find();
				$data['student'][$k]['content'] = $student_show['content'];
				foreach($state as $val){
					if($val['id'] == $student_show['state_id']){
						$data['student'][$k]['state_name'] = $val['name'];
					}
				}
			}
			$this->assign('state',$state);
			$this->assign('data',$data);
			//var_dump($data);
			$this->display();
		}
	}
	public function schedule_list(){
		$id = I('get.id','','int');
		$schedule = D('ScheduleView');
		$count      = $schedule->where('class_id='.$id)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$data = $schedule->where('class_id='.$id)->order('time asc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($data as $k=>$v){
			$data[$k]['headmaster_name'] = $this->teachername($v['headmaster_id']);
		}
		$this->assign('data',$data);
		$this->assign('page',$show);
		$this->display();
	}
	public function studentclass(){
		if(IS_POST){
			$student = M('sc_student');
			$data['class_id'] = I('post.class_id','','int');
			$all_student = I('post.all','','addslashes');
			$class = M('gradesclass');
			$class_list = $class->where('id='.$data['class_id'])->find();
			foreach($all_student as $k=>$v){
				$number = 0;
				$student_number = $student->where('up=1 and class_id='.$data['class_id'])->select();
				foreach($student_number as $val){
					$number += 1;
				}
				if($number<$class_list['enrollment_number']){
					if($student->where('id='.$v)->save($data) !== false) {
							$ss = true;
					}else {
							$ss = false;
					}
				}else{
					$this->success('班级人数已达上限',U('Classsetting/studentclass?id='.$data['class_id']));exit;
				}
			}			
			if($ss == true) {
					$this->success('添加学员成功',U('Classsetting/studentclass?id='.$data['class_id']));
			}else{
					$this->error('添加学员失败,班级人数已达上限!',U('Classsetting/studentclass?id='.$data['class_id']));
			}
		}else{
		$id = I('get.id','','int');
		$class_list = M('gradesclass')->where('id='.$id)->find();
		//课程分类
		$class_list['lecturename'] = $this->curname($class_list['lecture']);
		//主讲老师
		$class_list['teachername'] = $this->teachername($class_list['teacher_id']);
		//助教
		$class_list['headmastername'] = $this->teachername($class_list['headmaster_id']);
		//校区
		$class_list['xname'] = $this->basname($class_list['xid']);
		
		//课程相关未排班学生
		$student = M('sc_student')->where('class_id=0 and up=1 and curriculum_id='.$class_list['lecture'])->select();
		foreach($student as $k=>$v){
			$student[$k]['xs_name'] = $this->username($v['xsid']);
		}
		$this->assign('student',$student);
		//课程相关已排班学生
		$student_show = M('sc_student')->where('up=1 and curriculum_id='.$class_list['lecture'].' and class_id='.$id)->select();
		$number = 0;
		foreach($student_show as $k=>$v){
			$student_show[$k]['xs_name'] = $this->username($v['xsid']);
			$number += 1;
		}
		$class_list['number'] = $number;
		$this->assign('student_show',$student_show);
		$this->assign('class_list',$class_list);
		//var_dump($student);
		$this->display();
		}
	}
	//移除学员
	public function removestudent(){
		$classid = I('get.classid','','int');
		$id = I('get.id','','int');
		$student = M('sc_student');
		$data['class_id'] = 0;
		if($student->where('id='.$id)->save($data) !== false) {
				$this->success('移除学员成功',U('Classsetting/studentclass?id='.$classid));
		}else {
				$this->error('移除学员失败',U('Classsetting/studentclass?id='.$classid));
		}
	}
	//课程管理
	public function curriculum(){
		$curriculum = M('curriculum');
		if(IS_POST){
			$id = I('post.id','','int');
			if(empty($id)){
				if($curriculum->create()) {
						if($curriculum->add($_POST)) {
								$this->success('新增成功',U('Classsetting/curriculum'));
						}else {
								$this->error('新增失败',U('Classsetting/curriculum'));
						}
				}else{
					$this->error($curriculum->getError());
				}
			}else{
				if($curriculum->create()) {
						if($curriculum->where('id='.$id)->save($_POST) !== false) {
								$this->success('编辑成功',U('Classsetting/curriculum'));
						}else {
								$this->error('编辑失败',U('Classsetting/curriculum'));
						}
				}else {
						$this->error($curriculum->getError());
				}
			}
		}else{
			if(session('qe_user')['xid']!=0){
				$curriculum_data = $curriculum->where('xid='.session('qe_user')['xid'])->order('id desc')->select();
			}else{
				$curriculum_data = $curriculum->order('id desc')->select();
			}
			foreach($curriculum_data as $k=>$v){
				$curriculum_data[$k]['xname'] = $this->basname($v['xid']);
			}
			$this->assign('curriculum_data',$curriculum_data);
			//var_dump($curriculum_data);
			$this->display();
		}
	}
	public function curriculum_lookup(){
		$id = I('post.id','','int');
		$curriculum = M('curriculum')->where('id='.$id)->find();
		$this->ajaxReturn($curriculum);
	}
}